
class AnswerService {

  /**
   * 计算整个页面的分数，返回
   * @param page
   */
  countPageScore(page){
    if(page.sections && page.sections.length>0){
      let scores = []
      page.sections.forEach(sec=>{
        // options ,不同的问卷答案的方式不同
        switch (sec.type) {
          case 'checkbox':{
            this.countCheckBox(sec,scores)
            break
          }
          case 'radio':{
            this.countRadio(sec,scores)
            break
          }
          default:{

          }
        }
      })
      // 返回分数数组
      return  scores
    }else return null
  }
  countCheckBox(section,scores){
    // checkbox 有多个大胆
    if(section.answer,section.answer.length>0){
      section.answer.forEach(ak=>{
        let opt = section.options.find(o=>o.key === ak)
        // opt 正常下肯定在

        if(opt.tactics){
          opt.tactics.forEach(t=>{
            let score = scores.find(s=>s.tactic === t)
            if(!score) {
              score = {tactic: t, score: 0}
              scores.push(score)
            }
            score.score += parseInt(opt.point)
          })
        }
      })
    }
  }
  countRadio(section,scores){
    if(section.answer){
      console.log(section.answer,section.options)
      let opt = section.options.find(o=>o.key === section.answer)
      // opt 正常下肯定在

      if(opt.tactics){
        opt.tactics.forEach(t=>{
          let score = scores.find(s=>s.tactic === t)
          if(!score) {
            score = {tactic: t, score: 0}
            scores.push(score)
          }
          score.score += parseInt(opt.point)
        })
      }
    }
  }
  mergeScore(arr1,arr2){
    // 把 arr2 合并到 arr1里
    arr2.forEach(a2=>{
      let tmp = arr1.find(a1 => a1.tactic === a2.tactic)
      if(!tmp){
        tmp =  {tactic: a2.tactic, score: 0}
        arr1.push(tmp)
      }
      tmp.score += a2.score
    })
    return arr1
  }
}



export default new AnswerService()
